using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;

namespace JNPF.Basics.Controller
{
    [Route("api/Jiushi/[Controller]")]
    public class CustomerconsultantController : BaseApiController
    {
        private jiushiCustomerconsultantBll jiushiCustomerconsultantService = new jiushiCustomerconsultantBll();
        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();


        #region GET

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(int id)
        {
            var data = await jiushiCustomerconsultantService.GetInfoResult(id);
            var output = data.MapTo<jiushiCustomerconsultantListOutput>();
            return Success(output);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(jiushiCustomerconsultantListQueryInput input)
        {
            var data = await jiushiCustomerconsultantService.GetCusPageList(input);
            var outputList = data.list.MapToList<jiushiCustomerconsultantListOutput>();
            outputList.ForEach(o =>
            {
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }



        #endregion

        #region POST

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">新建输入</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] jiushiCustomerconsultantCrInput input)
        {
            if (!input.id.HasValue || input.id == null || input.id == 0)
            {

                //判断下当前这个公司，只能录入一次
                CustomerconsultantEntity customerconsultantEntity = await jiushiCustomerconsultantService.GetInfoByCustomerId(input.customerId.Value);

                if (customerconsultantEntity != null && customerconsultantEntity.Id > 0)
                {
                    return Error("当前客户已录入，不允许重复提交");
                }
            }
            await jiushiCustomerconsultantService.Create(input);
            return Success("新建成功");
        }


        /// <summary>
        /// 终止，合伙人要减一。
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpPost("End/{id}")]
        public async Task<ResponseResult> End(int id)
        {
            //
            await jiushiCustomerconsultantService.End(id);

            return Success("终止成功");
        }

        /// <summary>
        /// 审核积分
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpPost("CxamineCreate/{id}")]
        public async Task<ResponseResult> CxamineCreate(int id)
        {
            //
            await jiushiCustomerconsultantService.CxamineCreate(id);

            return Success("审核成功");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="input">编辑输入</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(int id, [FromBody] jiushiCustomerconsultantCrInput input)
        {
            var entity = input.MapTo<CustomerconsultantEntity>();
            var isOk = await jiushiCustomerconsultantService.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(int id)
        {
            var entity = await jiushiCustomerconsultantService.GetInfo(id);
            var isOk = await jiushiCustomerconsultantService.Delete(entity);
            return isOk > 0 ? Success("删除成功") : Error("删除失败");
        }

        #endregion

    }
}